
<?php
/*
 * 		author GuoLong
 * 		2014/12/03
 * 		email:guolong@howdo.cc
 */
class ItemController extends Controller {

	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
	}
	//test  
	public function Index(){
		$this->view->display("index.htm");
	}
	
	//获取题目网页
	public function getitemview(){
		$mode = $this->getRequest()->get('mode');
		$item_id = $this->getRequest()->get('item_id');
		$type = $this->getRequest()->get('type');//0- 题目内容 1-题目详情
		$width = $this->getRequest()->get('width')?$this->getRequest()->get('width'):512;
		$height = $this->getRequest()->get('height')?$this->getRequest()->get('height'):1350;
		$height = 1350;
		$item = new item();
		$result = $item->getItemJsonById($item_id);
		$json = $result['json'];
		$displayUtil = $this->getApp()->loadUtilClass('DisplayItemUtil');
		$displayUtil->init($mode,$type,$json,$width,$height);
		$displayUtil->display();
	}
	
	//获取题目图片url
	public function getItemImageUrl(){
		$item_id = $this->getRequest()->get('item_id');
		if($item_id){
			$qiniu_path=$this->getApp()->loadConf('qiniupath');
			$url =  $qiniu_path["qiniu_path_image"].'20150105item'.$item_id.'.jpg';
			$this->view->setState("1");
			$this->view->setData($url);
			$this->view->setMsg ( "获取成功" );
		}else{
			$this->view->setState("0");
			$this->view->setData((object)null);
			$this->view->setMsg ( "参数缺失~" );
		}
		$this->view->display('json');
	}
	//根据题目id获取老师的历史视频
	public function getItemVideo(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		$item_id = $this->getRequest()->get('item_id');
		$video=new video();
		$list=$video->teaGetVideoIdByItem($user_id,$item_id);
		if($list){
			$data=array("list"=>$list,"totalCount"=>count($list));
			$this->view->setState("1");
			$this->view->setMsg ( "获取成功" );
		}else{
			$data=null;
			$this->view->setState("0");
			$this->view->setMsg ( "没有更多了哟" );
		}
			$this->view->setData($data);
		$this->view->display('json');
	}
	public function delItemVideo(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		$video_id = $this->getRequest()->get('video_id');
		//根据题目id获取老师的历史视频
		$item=new item();
		if($item->isMyVideo($user_id, $video_id)){
			$result=$item->delVideoId($user_id,$video_id);
			if($result){
				$this->view->setData(null);
				$this->view->setState("1");
				$this->view->setMsg ( "删除成功" );
			}else{
				$this->view->setData(null);
				$this->view->setState("0");
				$this->view->setMsg ( "删除失败" );
			}
		}else{
			$this->view->setData(null);
			$this->view->setState("0");
			$this->view->setMsg ( " 删除失败，不是您的视频！" );
		}
		$this->view->display('json');
	}
	//反馈题目会与不会
// 	http://localhost/xdfServer/clientapi1.php/item/ItemFeedback/item_id/2/homework_id/1/feedback/0
	public function itemFeedback(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		$item_id=$this->getRequest()->get("item_id");
		$feedback=$this->getRequest()->get("feedback");
		$homework_id=$this->getRequest()->get("homework_id");
		
		$item_feedback=new item_feedback();
		$successflag=false;
		//$homework_id为0,为从错题本中删除题目
		if($homework_id=="0"){
			if($item_feedback->checkfeedback($user_id,$item_id)){
				$this->view->setState ( "1" );
				$this->view->setStatus ( "1" );
				$this->view->setMsg ( "移除成功！" );
			}else{
				$this->view->setStatus ( "1" );
				$this->view->setState ( "0" );
				$this->view->setMsg ( "移除失败" );
			}
		}else{ 
			if($item_feedback->feedback($feedback, $user_id, $homework_id, $item_id)){
				$this->view->setState ( "1" );
				$this->view->setStatus ( "1" );
				$this->view->setMsg ( "提交反馈成功！" );
			}else{
				$this->view->setStatus ( "1" );
				$this->view->setState ( "0" );
				$this->view->setMsg ( "提交反馈失败" );
			}
		}
		$this->view->display ( "json" );
	}
	/*
	 * 收藏题目
	 * item_id,为题目id用下划线连接的zifuc
	 */
	
// 	http://localhost/xdfServer/clientapi1.php/item/collectitem/item_id/2/desc/ddddddd
	public function collectItem(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		//$user_id = 5;
		$item_id_str=$this->getRequest()->get("item_id");
		$plant=$this->getRequest()->get("plant");
		if($plant=="stu"){
			$this->stuCollectItem($user_id,$item_id_str);
		}else if($plant=="tea"){
			$this->teaCollectItem($user_id,$item_id_str);
		}else{
			//$this->view->setMsg("plant参数错误！");
			$this->view->setMsg("失败了哟");
			$this->view->setState ( "0" );
			$this->view->display ( "json" );
		}
	}

	public function delItemFromCollection(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		$item_id=$this->getRequest()->get("item_id");
		$plant=$this->getRequest()->get("plant");
		if($plant=="stu"){
			$this->stuDelItemFromCollection($user_id,$item_id);
		}else if($plant=="tea"){
			$this->teaDelItemFromCollection($user_id,$item_id);
		}
	}
	
	//学生端收藏题目（单个题目）
	private function stuCollectItem($user_id,$item_id_str){
		
		//$item_id=json_decode($item_id_str,true);
// 		$item_id = explode('_', $item_id_str);/
		$itemcollection=new itemcollection();
		//判断是否已经收藏
		$this->view->setStatus ( "1" );
		$this->view->setState ( "1" );
		$msg="";
		$isCollectedFlag=false;
		$collectSuccessFlag=true;
		$val = $item_id_str;
		if($itemcollection->getCollectionItem($user_id, $val)){
			///$msg.= "-已经收藏了 id=".$val." 的题目！-" ;
			$msg.= "-已经收藏了哟" ;
			$isCollectedFlag=true;
			$this->view->setMsg($msg);
			$this->view->setState ( "0" );
			$this->view->display ( "json" );
			exit;
		}
		if(!$isCollectedFlag){
				if(!$itemcollection->collectItem($user_id, $val)){
					$collectSuccessFlag=false;
					//$msg.= "-收藏 id=".$val." 的题目失败！-" ;
					$msg.= "收藏 失败了哟！" ;
			}
		}else{
			$collectSuccessFlag=false;
		}
			
		if($collectSuccessFlag){
			$msg.="收藏成功！";
		}
		$this->view->setMsg($msg);
		$this->view->setData (null);
		$this->view->display ( "json" );
	}
	//教师端添加题目到收藏（传递数组）
	private function teaCollectItem($user_id,$item_id_str){
		//$item_id_str = urldecode($item_id_str);
		$item_id=json_decode($item_id_str,true);
		// 		$item_id = explode('_', $item_id_str);/
		$itemcollection=new itemcollection();
		//判断是否已经收藏
		$this->view->setStatus ( "1" );
		$this->view->setState ( "1" );
		$msg="";
		$data=array();
		$data["item_id_str"]=$item_id_str;
		$collectSuccessFlag=true;
		foreach($item_id as $key=>$val)
			if($itemcollection->getCollectionItem($user_id, $val)){
// 				$collectSuccessFlag = false;
// 				$msg.= "第".($key+1)."题已经存在于您的收藏中\n" ;
				continue;
			}else{
				if(!$itemcollection->collectItem($user_id, $val)){
					$collectSuccessFlag = false;
					//$msg.= "收藏第".($key+1)."题目失败\n" ;
					$msg.= "收藏第".($key+1)."题目失败\n" ;
				}
			}		
		if($collectSuccessFlag){
			$msg.="全部题目收藏成功！";
			$this->view->setMsg($msg);
			$this->view->setData ($data);
		}else{
			$msg.="其余题目收藏成功";
			$this->view->setMsg($msg);
			$this->view->setData ($data);
		}
		$this->view->display ( "json" );
		
	}
	//教师端删除题目
	private function teaDelItemFromCollection($user_id,$item_id_str){
		
		$item_id=json_decode($item_id_str,true);
		// 		$item_id = explode('_', $item_id_str);/
		$itemcollection=new itemcollection();
		
		
		//判断是否已经收藏   
		$msg="";
		$data=array();
				
		$this->view->setStatus ( "1" );
		$this->view->setState ( "1" );

		
		if(!is_array($item_id)){
			$msg="para error！！！";
			$this->view->setMsg ( $msg );
			$this->view->setState ( "0" );
			$this->view->setData($data);
			$this->view->display ( "json" );
			return;
		}
		$isCollectedFlag=false;
		$collectSuccessFlag=true;
		foreach($item_id as $key=>$val){
			$item_ids=$itemcollection->getCollectionItem($user_id, $item_id[$key]);
// 			if(is_array($item_ids))
			if(!is_array($item_ids)){
					//$msg.= "id=".$item_id[$key]." item not exist in your collection" ;
					$msg.= " 题目不在您的收藏列表噢" ;
					$isCollectedFlag=true;
					$collectSuccessFlag=false;
					$this->view->setMsg($msg);
					$this->view->setState ( "-1" );
					$this->view->display ( "json" );
					return;
				}
		}
		if(!$isCollectedFlag){
			foreach($item_id as $key=>$val){
				if(!$itemcollection->delItemFromCollection($user_id, $item_id[$key])){
					$collectSuccessFlag=false;
					//$msg.= " id=".$item_id[$key]." del failure" ;
					$msg.= "删除失败了哟" ;
					$this->view->setMsg($msg);
					$this->view->setState ( "-2" );
					$this->view->display ( "json" );
					return;
				}
			}
		}
		if($collectSuccessFlag){
			$msg.="移除成功！";
		}
		$this->view->setMsg($msg);
		$this->view->setData (null);
		$this->view->display ( "json" );
	}
	private function stuDelItemFromCollection($user_id,$item_id){
		$itemcollection=new itemcollection();
		if($itemcollection->getCollectionItem($user_id, $item_id)){
			if($itemcollection->delItemFromCollection($user_id, $item_id)){
				
				$this->view->setState ( "1" );
				$this->view->setMsg ( "操作成功" );
			}else{
				$this->view->setState ( "0" );
				$this->view->setMsg ( "操作失败" );
			}
		}else{
			$this->view->setState ( "0" );
			$this->view->setStatus ( "1" );
			$this->view->setMsg ( "此题目尚未收藏" );
		}
		$this->view->display ( "json" );
	}
	/*搜索题目
	 * type=1，我的搜藏夹
	 * type=2，系统题库
	 * subject_id
	 * difficultyd
	 * itemtype_id
	 * knowledge_id
	 * skill_id
	 * type
	 * pagesize
	 * timestamp
	 * action/more
	 */
	
// 	http://localhost/xdfServer/clientapi1.php/item/searchItem/pagesize/10/timestamp/100/action/pre
	public function searchItem(){
		$user_info = $this->getData ( 'userinfo' );
		$user_id = $user_info ['id'];
		//获取筛选条件
		$subject_id	= $this->getRequest()->get("subject_id");
// 		$difficulty_id = $this->getRequest()->get("difficulty");
		//已经删除了难度选择
		$difficulty_id = "0";
		$itemtype_id = $this->getRequest()->get("itemtype_id");
		($knowledge_id = $this->getRequest()->get("knowledge_id")) || ($knowledge_id = 0);
		($skill_id = $this->getRequest()->get("skill_id")) || ($skill_id = 0);
		($type = $this->getRequest()->get("type"))||($type = 0);//0,系统题库    1,我的题库
		$pagesize = $this->getRequest()->get("pagesize");
		$timestamp = $this->getRequest()->get("timestamp");
		$action = $this->getRequest()->get("action");
		
		$keyword=trim(urldecode($this->getRequest()->get("key")));

		$subject_id=($subject_id)?$subject_id:"0";
		$itemtype_id=($itemtype_id)?json_decode ($itemtype_id,true):"0";
		$knowledge=new knowledge();
		//获取知识节点下所有子节点
		if($knowledge_id){
			$knowledge_id=json_decode ($knowledge_id,true);
			$knowledge_id_array=array();
			foreach($knowledge_id as $key=>$val){
				$knowledge_id_array = array_merge($knowledge_id_array,$knowledge->getKnowsChildren($val,0) );
			}
			foreach($knowledge_id_array as $key=>$val){
				$knowledge_id[]=$val["id"];
			}
		}
		//获取技术节点下所有子节点
		if($skill_id){
			$skill_id=json_decode ($skill_id,true);
			$skill_id_array=array();
			foreach($skill_id as $key=>$val){
				$skill_id_array = array_merge($skill_id_array,$knowledge->getKnowsChildren($val,1) );
			}
			foreach($skill_id_array as $key=>$val){
				$skill_id[]=$val["id"];
			}
		}
		$item=new item();
		$item_ids="0";
		if($keyword == "0" || $keyword=="" ||$keyword==null){
			if($type=="1"){
				//获取收藏夹
				$list = $item->siftItem($user_id, $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$timestamp,$pagesize,$action,$item_ids);
			}else if($type=="0"){ 
				//获取系统题库
				$list = $item->siftItem("0", $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$timestamp,$pagesize,$action,$item_ids);
			} 

		} else{
			//先做筛选
			$_timestamp="0";
			$_pagesize="100000";
			$_action="pre";
			if($type=="1"){
				//获取收藏夹
				$list = $item->siftItem($user_id, $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$_timestamp,$_pagesize,$_action);
			}else if($type=="0"){
				//获取系统题库
				$list = $item->siftItem("0", $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$_timestamp,$_pagesize,$_action);
			} 
			//获取到筛选到的题目id
			$item_ids=array();
			if(is_array($list)){
				foreach($list as $key =>$val){
					$item_ids[]=$val["item_id"];
				}
			unset($list);
			//获取含有关键字的题目
			$usedKeyNum=5;$webtest=false;	
			$list=$this->searchItemByKeyword_new($keyword, $subject_id, $action, $timestamp, $pagesize,$item_ids,$usedKeyNum,$webtest);
			if(is_array($list)){
				foreach($list as $key =>$val){
					unset($list[$key]["value"]);
				}
			}
				
			}
		}

		if(is_array($list) && count($list)<>0){
			foreach($list as $key=>$val){
				$itemcollection=new itemcollection();
				$item_id=$list[$key]["item_id"] ;
				$list[$key]["iscollected"]=($itemcollection->getCollectionItem($user_id, $item_id))?"1":"0";
			}
				
			$totalCount = count($list);
			$data = array('list'=>$list,'totalCount'=>$totalCount);
			$this->view->setData ( $data );
			$this->view->setState ( "1" );
			$this->view->setMsg ( "成功了哟" );
		}else{
			$this->view->setData ( null );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "没有更多数据了哟！" );
		}
		$this->view->display ( "json" );
	}
// 	http://localhost/xdfServer/clientapi1.php/item/searchItem/pagesize/10/timestamp/100/action/pre
	private function siftItem(
			$user_id,
			$subject_id,$difficulty_id,
			$difficulty_id,$itemtype_id,$knowledge_id,$skill_id,
			$type,$pagesize,$timestamp,$action
		){
		$item=new item();
		if($type=="1"){
			//获取收藏夹
			$list = $item->siftItem($user_id, $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$timestamp,$pagesize,$action);
		}else if($type=="0"){ 
			//获取系统题库
			$list = $item->siftItem("0", $subject_id,$difficulty_id, $itemtype_id, $knowledge_id, $skill_id,$timestamp,$pagesize,$action);
		} 
		return $list;
	}
	//test
	// 		$webtest=false;
	//设置搜索关键字的个数
	// 		$usedKeyNum=5;
// 	http://119.254.108.124:10023/clientapi.php/item/searchItemByKeyword/key/123/subject_id/0/action/pre/timestamp/100/pagesize/100

	private function searchItemByKeyword_new($key, $subid, $pageType, $timestamp, $pageSize,$item_ids,$usedKeyNum,$webtest) {
			//test
		$sumTime["startTime"]=date('H:i:s.ms',time());
		/* 分词处理，将整个str处理成以空格为分割符的str */
		$util = $this->_app->loadUtilClass("StringUtil");
		$key = $util->split_word ( trim ( strip_tags ( $key ) ) );
		$keywords = explode ( ",", trim ( $key ) );
		//删除分词中的空元素
		array_filter($keywords);
		unset ( $key );
		$keys=array();
		foreach($keywords as $key=>$val){
			if($val<>"")
				$keys[]=$val;
		}
// 		var_dump($keys);
		$list = array ();
	
		$count = 0;
		// $flag = false;
		$item=new item();
		if(!$usedKeyNum)
			$num=count( $keys );
		else 
			$num=min(count( $keys ),$usedKeyNum);
		for($i = 0; $i < $num; $i ++) {
			$templist = $item->sertchItemBySubjectAndIds(trim ( $keys [$i] ), $subid,$item_ids,100000, 0 );
			  
// 			$templist = $item->searchAllBySubject ( trim ( $keys [$i] ), $subid, 100000, 0 );
			if ($templist) {
				for($k = 0; $k < count ( $templist ); $k ++) {
					$templist [$k] ['content'] = $util->html2utf8 ( $templist [$k] ['content'] );
					$templist [$k] ["content"] = mb_substr($templist [$k] ['content'], 0, 40, 'utf-8');

				}
				$list = array_merge ( $list, $templist );
			}
		}
		//sumtime
		$sumTime["queryTime"]=date('H:i:s.ms',time());
		if (count ( $list ) > 0) {
			// 新去重方法
			$data ['list'] = array ();
			$alreadyExist = array ();
			for($i = 0; $i < count ( $list ); $i ++) {
				if (! array_key_exists ( $list [$i] ['item_id'], $alreadyExist )) {
					$position = array_push ( $data ['list'], $list [$i] );
					// $t = strip_tags ( $list [$i] ['content'] );
					// $t = str_replace ( "&nbsp;", " ", $t );
					$data ['list'] [$position] ['content'] = $list [$i] ['content'];
					$data ['list'] [$position] ['item_id'] = $list [$i] ['item_id'];

					$data ['list'] [$position] ['value'] = 0;
					$alreadyExist [$list [$i] ['item_id']] = $position;
				} else {
					$data ['list'] [$alreadyExist [$list [$i] ['item_id']]] ['value'] ++;
				}
			}
	
			// 按相关度从大到小排序
			$data ['list'] = $this->quicksort ( $data ['list'] );
			for($i = 0; $i < count ( $data ['list'] ); $i ++) {
				$data ['list'] [$i] ['timestamp'] = $i + 1;
			}
			$viewdata["list"]=array();
			// 根据timestamp、pagetype、pagesize筛选数据
			if ($pageType == "pre" ) {
				// 取最新pagesize条
				for($i = 0; $i < $pageSize && $i < count ( $data ['list'] ); $i ++) {
					$viewdata ['list'] [$i] = $data ['list'] [$i];
				}
			} else if ($pageType == "more") {
				// 取timestamp开头（包括）的pagesize条
				for($i = 0; $i < $pageSize && ($i+$timestamp) < count ( $data ['list'] ); $i ++) {
					$viewdata ['list'] [$i] = $data ['list'] [$timestamp + $i];
				}
			}
			$viewdata ['totalCount'] = "" . count ( $viewdata ['list'] );

			//sumtime
			$sumTime["sortTime"]=date('H:i:s.ms',time());
			if($webtest)var_dump($sumTime);
			return $viewdata['list'];
		} else {
			return false;
		}
	}
// 	http://119.254.108.124:10023/clientapi.php/item/searchItemByKeyword/key/123/subject_id/0/action/pre/timestamp/100/pagesize/100
	public function searchItemByKeyword(){
		
		$key=$this->getRequest()->get("key");
		$type = $this->getRequest()->get("type");//0,系统题库    1,我的题库
		$subid=$this->getRequest()->get("subject_id");
		$pageType=$this->getRequest()->get("action");
		$timestamp=$this->getRequest()->get("timestamp");
		$pageSize=$this->getRequest()->get("pagesize");
		
		//test
		$webtest=false;
		//设置搜索关键字的个数
		$usedKeyNum=5;
		//返回消息
		$msg="";
		//连接搜索接口
		$data= $this->searchExercise($key, $subid, $pageType, $timestamp, $pageSize,$usedKeyNum,$webtest);
		if($data){
			$msg.="successfully search items!";
			$this->view->setState ( "1" );
		}else{
			$msg.="no items!";
			$this->view->setState ( "0" );
			$this->view->setMsg ( "没有更多数据了哟!" );
		}
		$this->view->setMsg ( $msg );
		$this->view->setData ( $data );
		$this->view->display ( "json" );
	}
	private function searchExercise($key, $subid, $pageType, $timestamp, $pageSize,$usedKeyNum,$webtest) {
		//test
		$sumTime["startTime"]=date('H:i:s.ms',time());
		/* 分词处理，将整个str处理成以空格为分割符的str */
		$util = $this->_app->loadUtilClass("StringUtil");
		$key = $util->split_word ( trim ( strip_tags ( $key ) ) );
		$keys = explode ( ",", trim ( $key ) );
		//删除分词中的空元素
		array_filter($keys);
		unset ( $key );
	
		$list = array ();
	
		$count = 0;
		// $flag = false;
		$item=new item();
		if(!$usedKeyNum)
			$num=count( $keys );
		else 
			$num=min(count( $keys ),$usedKeyNum);
		for($i = 0; $i < $num; $i ++) {
			$templist = $item->searchAllBySubject ( trim ( $keys [$i] ), $subid, 100000, 0 );
			if ($templist) {
				for($k = 0; $k < count ( $templist ); $k ++) {
					$templist [$k] ['content'] = $util->html2utf8 ( $templist [$k] ['content'] );
				}
				$list = array_merge ( $list, $templist );
			}
		}
		//sumtime
		$sumTime["queryTime"]=date('H:i:s.ms',time());
		if (count ( $list ) > 0) {
			// 新去重方法
			$data ['list'] = array ();
			$alreadyExist = array ();
			for($i = 0; $i < count ( $list ); $i ++) {
				if (! array_key_exists ( $list [$i] ['id'], $alreadyExist )) {
					$position = array_push ( $data ['list'], $list [$i] );
					// $t = strip_tags ( $list [$i] ['content'] );
					// $t = str_replace ( "&nbsp;", " ", $t );
					$data ['list'] [$position] ['content'] = $list [$i] ['content'];
					$data ['list'] [$position] ['id'] = $list [$i] ['id'];

					$data ['list'] [$position] ['value'] = 0;
					$alreadyExist [$list [$i] ['id']] = $position;
				} else {
					$data ['list'] [$alreadyExist [$list [$i] ['id']]] ['value'] ++;
				}
			}
	
			// 按相关度从大到小排序
			$data ['list'] = $this->quicksort ( $data ['list'] );
			for($i = 0; $i < count ( $data ['list'] ); $i ++) {
				$data ['list'] [$i] ['timestamp'] = $i + 1;
			}
			$viewdata["list"]=array();
			// 根据timestamp、pagetype、pagesize筛选数据
			if ($pageType == "pre" ) {
				// 取最新pagesize条
				for($i = 0; $i < $pageSize && $i < count ( $data ['list'] ); $i ++) {
					$viewdata ['list'] [$i] = $data ['list'] [$i];
				}
			} else if ($pageType == "more") {
				// 取timestamp开头（包括）的pagesize条
				for($i = 0; $i < $pageSize && $i < count ( $data ['list'] ); $i ++) {
					$viewdata ['list'] [$i] = $data ['list'] [$timestamp + $i];
				}
			}
			$viewdata ['totalCount'] = "" . count ( $viewdata ['list'] );

			//sumtime
			$sumTime["sortTime"]=date('H:i:s.ms',time());
			if($webtest)var_dump($sumTime);
			return $viewdata;
		} else {
			return false;
		}
	}
	
	private function quicksort($array) {
		$count = count ( $array );
		if ($count <= 1)
			return $array;
		if (isset ( $array [0] ['value'] )) {
			$key = $array [0] ['value'];
		} else {
			$key = 0;
		}
		$left_array = array ();
		$middle_array = array ();
		$right_array = array ();
	
		foreach ( $array as $each ) {
			if (! isset ( $each ['value'] )) {
				continue;
			}
			if ($key < $each ['value']) {
				array_push ( $left_array, $each );
			} else if ($key == $each ['value']) {
				$middle_array [] = $each;
			} else {
				$right_array [] = $each;
			}
		}
		$left_array = $this->quicksort ( $left_array );
		$right_array = $this->quicksort ( $right_array );
	
		$array = array_merge ( $left_array, $middle_array, $right_array );

		return $array;
	}
}

